#!/usr/local/python
import random
import euclid
import events.dispatcher
import examples.basegame
import game.entity
import ai.steer.engine as steer
import pygame
import render

class FlockBoid(game.entity.Boid):
    def __init__(self, window,pos,  mass, maxSpeed, maxForce, maxTurnRate):
        super(FlockBoid, self).__init__( window,pos,  mass, maxSpeed, maxForce, maxTurnRate )
        self.sep = steer.Separation( 150, 2, 100 )
        self.align = steer.Alignment( 150, 2, 100 )
        self.cohesion = steer.Cohesion(150, 2, 100)
        self.wander = steer.Wander( 30, 40, 5 )

        self.steer.add( self.sep )
        self.steer.add( self.align )
        self.steer.add( self.cohesion )
        self.steer.add( self.wander )

    def draw( self ):
        super(FlockBoid, self).draw()


class FlockDemo(examples.basegame.BaseGame):
    def __init__(self):
        super(FlockDemo, self).__init__()
        random.seed()
        self.boids = []

    def setup(self):
        super(FlockDemo, self).setup()

        for i in range( 0, 10 ):
            pos = euclid.Vector3( random.uniform( 0, 700 ), random.uniform( 0, 700 ) )
            self.boids.append( FlockBoid(self.window, pos, 10, 50, 500, 500 ) )

    def _loop(self):
        super(FlockDemo, self)._loop()
        b = self.boids[0]
        render.textrenderer.instance().render("Separation: %d Align: %d Cohesion: %d"
        %(b.sep.weight, b.align.weight, b.cohesion.weight), (100, 10))



    def _onKey(self, event):
        if event.key == pygame.K_q:
            for b in self.boids:
                b.sep.weight += 100
        elif event.key == pygame.K_a:
            for b in self.boids:
                b.sep.weight = max( 0, b.sep.weight - 100 )
        elif event.key == pygame.K_w:
            for b in self.boids:
                b.align.weight += 100
        elif event.key == pygame.K_s:
            for b in self.boids:
                b.align.weight = max(0, b.align.weight - 100 )
        elif event.key == pygame.K_e:
            for b in self.boids:
                b.cohesion.weight += 100
        elif event.key == pygame.K_d:
            for b in self.boids:
                b.cohesion.weight = max(0, b.cohesion.weight - 100)
        super( FlockDemo, self )._onKey( event )
